package com.lc.zpyh.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.widget.view.ClearEditText;
import com.lc.widget.view.CountdownView;
import com.lc.widget.view.RegexEditText;
import com.lc.zpyh.R;
import com.lc.zpyh.app.AppActivity;
import com.lc.zpyh.http.request.BindPushRidApi;
import com.lc.zpyh.http.request.PublicMsgBean;
import com.lc.zpyh.http.request.QQverificationCodeApi;
import com.lc.zpyh.http.request.SendSmsApi;
import com.lc.zpyh.http.request.TheInvitationCodeApi;
import com.lc.zpyh.http.request.UserWXBindMobileApi;
import com.lc.zpyh.http.response.QQBindBean;
import com.lc.zpyh.http.response.TheInvitationCodeBean;
import com.lc.zpyh.http.response.UserWXBindMobile;
import com.lc.zpyh.http.response.VerificationCodeBean;
import com.lc.zpyh.other.IntentKey;
import com.lc.zpyh.util.SPUtil;
import com.mob.pushsdk.MobPush;

import java.util.UUID;

import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.AppCompatEditText;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class BindMobilePhoneNumberActivity extends AppActivity {
    @BindView(R.id.et_phone_reset_phone)
    RegexEditText etPhoneResetPhone;
    @BindView(R.id.et_phone_reset_code)
    AppCompatEditText etPhoneResetCode;
    @BindView(R.id.cv_phone_reset_countdown)
    CountdownView cvPhoneResetCountdown;
    @BindView(R.id.et_invit_code)
    RegexEditText etInvitCode;
    @BindView(R.id.btn_phone_reset_commit)
    AppCompatButton btnPhoneResetCommit;
    @BindView(R.id.et_password)
    ClearEditText etPassword;
    @BindView(R.id.et_password_agine)
    ClearEditText etPasswordAgine;
    private String type;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_bindmobilephonenumber;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        type = getIntent().getStringExtra("type");
    }

    @OnClick({R.id.cv_phone_reset_countdown, R.id.btn_phone_reset_commit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cv_phone_reset_countdown:

                if (etPhoneResetPhone.getText().toString().length() != 11) {
                    etPhoneResetPhone.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.shake_anim));
                    toast(R.string.common_phone_input_error);
                    return;
                }
                if (etPhoneResetPhone.getText().toString().isEmpty()) {
                    etPhoneResetPhone.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.shake_anim));
                    toast("手机号不能为空");
                    return;
                }

                // 获取验证码
                EasyHttp.post(this)
                        .api(new SendSmsApi()
                                .setPhone(etPhoneResetPhone.getText().toString())).request(new HttpCallback<PublicMsgBean>(this) {

                    @Override
                    public void onStart(Call call) {
                        cvPhoneResetCountdown.start();
                    }

                    @Override
                    public void onEnd(Call call) {

                    }

                    @Override
                    public void onSucceed(PublicMsgBean data) {

                        toast(data.getList());
                    }

                    @Override
                    public void onFail(Exception e) {
                        super.onFail(e);

                    }
                });
                break;
            case R.id.btn_phone_reset_commit:

                if (etInvitCode.getText().toString().isEmpty()) {
                    if (etPhoneResetPhone.getText().toString().length() != 11) {

                        toast(R.string.common_phone_input_error);
                        return;
                    }
                    if (etPhoneResetPhone.getText().toString().isEmpty()) {

                        toast("手机号不能为空");
                        return;
                    }
                    if (etPhoneResetCode.getText().toString().isEmpty()) {

                        toast("验证码不能为空");
                        return;
                    }
                    if (etPassword.getText().toString().isEmpty()) {

                        toast(R.string.common_password_input_error);
                        return;
                    }
                    if (etPasswordAgine.getText().toString().isEmpty()) {

                        toast(R.string.common_password_input_error);
                        return;
                    }
                    if (!etPassword.getText().toString().trim().equals(etPasswordAgine.getText().toString().trim())){
                        toast(R.string.common_password_input_unlike);
                        return;
                    }
                    if (type.equals("wx")) {
                        userWXBindMobile();
                    } else {
                        QQverificationCode();
                    }

                } else {
                    invit();
                }
                break;
        }
    }

    private void QQverificationCode() {
        EasyHttp.post(this)
                .api(new QQverificationCodeApi()
                        .setPhone(etPhoneResetPhone.getText().toString()).setCode(etPhoneResetCode.getText().toString()).setUserId(SPUtil.get(this, IntentKey.USERID, "")).setPassWord(etPassword.getText().toString().trim())).request(new HttpCallback<QQBindBean>(this) {


            @Override
            public void onSucceed(QQBindBean data) {
                if (data.getCode() == 0 || data.getCode() == 10) {
                    SPUtil.put(BindMobilePhoneNumberActivity.this, IntentKey.PHONE, data.getList().getUserPhone());
                    SPUtil.put(BindMobilePhoneNumberActivity.this, IntentKey.ACCOUNT, data.getList().getUserAccount());
//                SPUtil.put(LoginCodeActivity.this, IntentKey.TOKEN, data.getList().getUserInfo());
                    SPUtil.put(BindMobilePhoneNumberActivity.this, IntentKey.INVITECODE, data.getList().getInviteCode());
                    SPUtil.put(BindMobilePhoneNumberActivity.this, IntentKey.USERID, data.getList().getUserId() + "");
                    SPUtil.put(BindMobilePhoneNumberActivity.this, IntentKey.USERQQPHONE, data.getList().getUserPhone());
                    SPUtil.put(BindMobilePhoneNumberActivity.this, IntentKey.SEX, data.getList().getUserGender() + "");
                    bindPushRid(data.getList().getUserId() + "", "");
                    toast(data.getMsg());

                } else {

                    toast(data.getMsg());

                }
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });
    }

    private void invit() {
        EasyHttp.post(this)
                .api(new TheInvitationCodeApi()
                        .setUserId(SPUtil.get(this, IntentKey.USERID, "")).setInvitationCode(etInvitCode.getText().toString())).request(new HttpCallback<TheInvitationCodeBean>(this) {

            @Override
            public void onSucceed(TheInvitationCodeBean data) {
                if (data.getCode() == 0) {
                    if (type.equals("wx")) {
                        userWXBindMobile();
                    } else {
                        QQverificationCode();
                    }
                } else {
                    toast(data.getList());
                }

            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });

    }

    private void userWXBindMobile() {
        EasyHttp.post(this)
                .api(new UserWXBindMobileApi()
                        .setPhone(etPhoneResetPhone.getText().toString()).setCode(etPhoneResetCode.getText().toString()).setHeadimgurl(SPUtil.get(this, IntentKey.HEADIMGURL, "")).setNickname(SPUtil.get(this, IntentKey.NICKNAME, "")).setOpenid(SPUtil.get(this, IntentKey.VXOPENID, "")).setSex(SPUtil.get(this, IntentKey.SEX, "")).setPassWord(etPassword.getText().toString().trim())).request(new HttpCallback<UserWXBindMobile>(this) {


            @Override
            public void onSucceed(UserWXBindMobile data) {
                if (data.getCode() == 0) {
//                    SPUtil.put(BindMobilePhoneNumberActivity.this, IntentKey.OPENID, id);
                    SPUtil.put(BindMobilePhoneNumberActivity.this, IntentKey.PHONE, data.getList().getUserInfo().getUserPhone());
                    SPUtil.put(BindMobilePhoneNumberActivity.this, IntentKey.ACCOUNT, data.getList().getUserInfo().getUserAccount());
//                SPUtil.put(LoginCodeActivity.this, IntentKey.TOKEN, data.getList().getUserInfo());
                    SPUtil.put(BindMobilePhoneNumberActivity.this, IntentKey.INVITECODE, data.getList().getUserInfo().getInviteCode());
                    SPUtil.put(BindMobilePhoneNumberActivity.this, IntentKey.USERID, data.getList().getUserInfo().getUserId() + "");
                    SPUtil.put(BindMobilePhoneNumberActivity.this, IntentKey.NEEDTOBINDMOBILEPHONENUMBER, "1");
                    bindPushRid(data.getList().getUserInfo().getUserId() + "", "");

                } else {

                    toast("验证错误");
                }

            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });
    }

    private void bindPushRid(String userid, String miaoyan) {
        MobPush.deleteAlias();
        UUID own = UUID.randomUUID();
        String uuid = own.toString();
        MobPush.setAlias(uuid);

        EasyHttp.post(this)
                .api(new BindPushRidApi()
                        .setUserId(userid)
                        .setRid(uuid)
                ).request(new HttpCallback<VerificationCodeBean>(this) {


            @Override
            public void onSucceed(VerificationCodeBean data) {

                finish();
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });
    }
}
